var app = app || {};

app.Navigation = {
    init: function () {
        this.initView();
        this.initClickEvents();
        this.initJoinView();
        app.CreateView.initCreateForm();
        $('#createView').hide();
        $('#joinView').hide();
    },

    initClickEvents: function () {
        $('#leaveTournament').click(this.leaveTournament);
        $('#quitTournament').click(this.quitTournament);
        $("#btn_homeView").click(function () {
            $("#joinView").hide();
            $("#createView").hide();
            $("#homeView").show();
        });
        $("#btn_createView").click(function () {
            $("#homeView").hide();
            $("#createView").show();
            $("#joinView").hide();
            //initValues();
        });
        $("#btn_joinView").click(function () {
            $("#homeView").hide();
            $("#createView").hide();
            $("#joinView").show();
        });

        $('#reitPlatz').click(function () {
            $('#reitBaum').removeClass("active");
            $('#reitTable').removeClass("active");
            $('#reitPlatz').addClass("active");
            $("#umgebung_platzierung").show("slow");
            $("#umgebung_tabelle").hide("slow");
            $("#umgebung_baum").hide("slow");
        });
        $("#reitTable").click(function () {
            $('#reitBaum').removeClass("active");
            $('#reitPlatz').removeClass("active");
            $('#reitTable').addClass("active");
            $("#umgebung_tabelle").show("slow");
            $("#umgebung_platzierung").hide("slow");
            $("#umgebung_baum").hide("slow");
        });
        $("#reitBaum").click(function () {
            $('#reitPlatz').removeClass("active");
            $('#reitTable').removeClass("active");
            $('#reitBaum').addClass("active");
            $("#umgebung_baum").show("slow");
            $("#umgebung_platzierung").hide("slow");
            $("#umgebung_tabelle").hide("slow");
        });
    },

    initView: function () {
        $('#tournamentView').hide();
        $('#quitTournament').hide();
        $('#btn_openModal').hide();
    },

    initJoinView: function () {
        $('#listofTournaments input').keydown(function (e) {
            var keycode = e.which || e.keyCode;
            if (keycode == 13) {
                e.preventDefault();
                ap.Navigation.adminExistingTournament();
            }
        });
        $('#listofTournaments form').submit(this.joinTournament);
        $('#joinTournament').click(this.joinTournament);
        $('#adminExistingTournament').click(this.adminExistingTournament);
    },

    initTournamentList: function (list) {
        $('#indexView').show("slow");
        $('#tournamentView').hide("slow");
        $('#quitTournament').hide();
        $('#btn_openModal').hide();
        //Unterseite Home
        if (!app.isTest) {
            $('#homeView').show();
            $('#joinView').hide();
            $('#createView').hide();
        } else {
            $('#homeView').hide();
            $('#joinView').show();
            $('#createView').hide();
        }
        $(".GameTable tr").each(function () {
            $(this).unbind()
        });
        //wenn noch keine Turnierie vorhanden sind
        if (list.length < 1) {
            $('#listofTournaments').children().hide();
            $('#listofTournaments h3').text('Noch kein Turnier erstellt. Bitte ein neues anlegen').show();
        }
        //wenn Turniere vorhanden sind
        else {
            $('#listofTournaments').children().show();
            $('#listofTournaments h3').text('Turnier beitreten oder Administrieren');
            $('#select_tournamentList').empty();
            $('#select_tournamentList').append(new Option('bitte Turnier wählen...', -1));
            for (var i = 0; i < list.length; i++) {
                var optionText = list[i].name + " (" + list[i].id + ")";
                $('#select_tournamentList').append(new Option(optionText, list[i].id));
            }
        }
    },

    refreshTournamentList: function (tournamentID, tournamentName) {
        //Turnier wurde geschlossen und wird aus der Liste gelöscht 
        if (tournamentName == undefined) {
            $('#select_tournamentList option[value ="' + tournamentID + '"]').remove();
            if ($('#select_tournamentList option').length <= 1) {
                $('#listofTournaments').children().hide();
                $('#listofTournaments h3').text('Noch kein Turnier erstellt. Bitte ein neues anlegen').show();
            }
        }
        //Turnier wurde erstellt und wir der lsite hinzugefügt
        else {
            var optionText = tournamentName + " (" + tournamentID + ")";
            $('#select_tournamentList').append(new Option(optionText, tournamentID));
            if ($('#select_tournamentList option').length > 1) {
                $('#listofTournaments').children().show();
                $('#listofTournaments h3').text('Turnier beitreten oder Administrieren');
            }
        }
    },

    initTournament: function () {
		$('#changelog').hide();
        $('#indexView').hide("slow");
        $('#tournamentView').show("slow");
        $('#reitBaum').removeClass("active");
        $('#reitPlatz').removeClass("active");
        $('#reitTable').addClass("active");
        $('#umgebung_tabelle').show();
        $('#umgebung_baum').hide();
        $('#umgebung_platzierung').hide();
        app.TournamentViews.recieveTournament();
    },

    gameover: function () {
        $('#changelog div a').each(function (index, value) {
            if (index > 0) {
                $(this).remove();
            }
        });
    },

    confirmTournamentAdmin: function () {
        console.log("Empfange: Du darfst das Turnier nun Bearbeiten");
        $('#quitTournament').show();
        $('#btn_openModal').show();
        app.TournamentViews.adminRecieveTournament();
    },

    accesDenied: function () {
        //Div Container, der eine Fehlermeldung enthält
    },

    showError: function (message) {
        //console.log(message);
        $('#errorMessage a').html(message);
        $('#errorMessage').modal('show');
    },

    recieveChange: function (data) {
		$('#changelog').show();
		var gamenumber = data.spiel + 1;
        var insertion = data.date + ": " + data.winner.name + " hat Spiel " + (parseInt(data.spiel) + 1) + " gewonnen. (" + data.res + ")";
		$('#changelog a:eq(1)').remove();
        $('#changelog a:eq(0)').after('<a class="list-group-item">' + insertion + '</a>');
        $('#changelog a:eq(1)').hide().show("slow");
		setTimeout(function() {$('#changelog').hide('slow');},5000);
    },

    joinTournament: function () {
        if ($("#select_tournamentList").val() != -1) {
            app.Client.joinTournament($("#select_tournamentList").val());
            $("#select_tournamentList").val(-1);
        } else {
            $('#errorMessage a').html('Bitte ein Turnier auswählen');
            $('#errorMessage').modal('show');
        }
    },

    adminExistingTournament: function () {
        if ($("#select_tournamentList").val() != -1) {
            app.Client.adminExistingTournament($('#select_tournamentList').val(), $('#password_admin_existing').val());
            $("#select_tournamentList").val(-1);
            $('#password_admin_existing').val('');
        } else {
            $('#errorMessage a').html('Bitte ein Turnier auswählen');
            $('#errorMessage').modal('show');
        }
    },

    leaveTournament: function () {
        $('#changelog div a').each(function (index, value) {
            if (index > 0) {
                $(this).remove();
            }
        });
        app.Client.leaveTournament();
    },

    quitTournament: function () {
        app.Client.quitTournament();
    },

};

app.TournamentViews = {

    recieveTournament: function () {
        var initGameTable = function () {
            document.getElementById("umgebung_tabelle").innerHTML = "<h1>Alle Begegnungen</h1>";
            var loserRound = app.Client.getLosersbracketRound(1);

            /*Spiele der Hauptrunde*/
            var firstGame = 0;
            var countGame = app.clientTournament.fieldSize / 2;
            app.TournamentViews.createGameTable("Hauptrunde", firstGame, countGame); //Verbesserung möglich: Mainround übergeben

            for (var i = 1; i <= app.clientTournament.losersbracketRoundNumber; i++) {
                var loserRound = app.Client.getLosersbracketRound(i);

                if ((i != app.clientTournament.losersbracketRoundNumber && app.clientTournament.fieldSize <= 32) || (i != app.clientTournament.losersbracketRoundNumber - 1 && app.clientTournament.fieldSize > 32)) {
                    /*Spiele der Verlierrunde i (bis i-1 bei 32er Feld: Viertelfinale)*/
                    var firstGame = loserRound[0];
                    var countGame = loserRound.length;
                    var round = "Verliererrunde " + i;
                    app.TournamentViews.createGameTable(round, firstGame, countGame);
                }

                if (i % 2 != 0) {
                    /*Spiele der Gewinnerrunde i/2*/
                    var winnerRound = app.Client.getWinnersbracketRound((i + 1) / 2);
                    var firstGame = winnerRound[0];
                    var countGame = winnerRound.length;
                    var round = "Gewinnerrunde " + (i + 1) / 2;
                    app.TournamentViews.createGameTable(round, firstGame, countGame);
                }

                if (i == app.clientTournament.losersbracketRoundNumber - 1 && app.clientTournament.fieldSize > 32) {
                    /*Viertelfinale*/
                    var winnerRound = app.Client.getWinnersbracketRound((i + 1) / 2);
                    var firstGame = loserRound[0];
                    var countGame = loserRound.length;
                    var round = "Viertelfinale";
                    app.TournamentViews.createGameTable(round, firstGame, countGame);
                }
                if (i == app.clientTournament.losersbracketRoundNumber) {
                    /*Halbfinale*/
                    var firstGame = loserRound[0];
                    var countGame = loserRound.length;
                    var round = "Halbfinale";
                    app.TournamentViews.createGameTable(round, firstGame, countGame);
                }
                if (i == app.clientTournament.losersbracketRoundNumber) {
                    /*Finale*/
                    var firstGame = app.clientTournament.final;
                    var countGame = 1;
                    var round = "Finale";
                    app.TournamentViews.createGameTable(round, firstGame, countGame);
                }
            }
        };
        var initEditModal = function () {
            document.getElementById("gamesSelection").innerHTML = "";
            var initoption = document.createElement("option");
            initoption.text = "Bitte wählen...";
            initoption.value = -1;
            $("#gamesSelection").append(initoption);

            for (var i = 0; i < app.clientTournament.gameList.length; i++) {
                var option = document.createElement("option");
                option.text = "Spiel " + (i + 1);
                option.value = i;
                $("#gamesSelection").append(option);
            }
            $("#gamesSelection").change(app.TournamentViews.showEdit);

            $("#btn_setScore").unbind("click");
            $("#btn_setScore").click(function (e) {
                e.preventDefault();
                app.Client.setScoreInGame($("#gamesSelection :selected").val(), $("#scoreP1").val(), $("#scoreP2").val());
            });
        };
        //$('#tournamentView div h2').html('test');
        $('#navbrandTournamentName').html(app.clientTournament.name);
        initGameTable();
        initEditModal();
        //Init GameTable
        console.log("refresh Turnier");
    },

    adminRecieveTournament: function () {

        $(".GameTable tr").each(function () {
            this.setAttribute("data-toggle", "modal");
            this.setAttribute("data-target", "#editModal");
        });
        $(".GameTable tr").click(app.TournamentViews.showEdit);
        $('#btn_openModal').click(app.TournamentViews.showEdit);
    },

    showEdit: function (e) {
        if (e.currentTarget.id === 'btn_openModal') {
            $("#gamesSelection").val(-1);
        } else if (this.getAttribute('number')) {
            $("#gamesSelection").val(this.getAttribute('number'));
        }

        document.getElementById("msgEdit").innerHTML = "";
        document.getElementById("scoreP1").style.backgroundColor = "#fff";
        document.getElementById("scoreP2").style.backgroundColor = "#fff";

        var gameNo = $("#gamesSelection").val();
        //Init-Option ausgewählt (-1)
        if (gameNo == -1) {
            $("#scoreP1").val("");
            $("#scoreP2").val("");
            $("#labelPlayer1").text("");
            $("#labelPlayer2").text("");
            document.getElementById("scoreP1").style.backgroundColor = "#ccc";
            document.getElementById("scoreP2").style.backgroundColor = "#ccc";
            var p = "<p class='text-warning'>Bitte ein Spiel auswählen.</p>"
            document.getElementById("msgEdit").innerHTML = p;
        } else {
            players = app.TournamentViews.getPlayer(gameNo);
            $("#labelPlayer1").text(players[0].textContent);
            $("#labelPlayer2").text(players[1].textContent);
            //Freilos im Spiel - nicht Bearbeiten
            if ((app.clientTournament.gameList[gameNo].player1 == -1 || app.clientTournament.gameList[gameNo].player2 == -1) || (app.clientTournament.gameList[gameNo].player1 == null || app.clientTournament.gameList[gameNo].player2 == null)) {
                var erg1 = app.clientTournament.gameList[gameNo].scorePlayer1;
                var erg2 = app.clientTournament.gameList[gameNo].scorePlayer2;
                parseInt(erg1);
                parseInt(erg2);
                $("#scoreP1").val(erg1);
                $("#scoreP2").val(erg2);
                var p = "<p class='text-danger'>Das Spiel " + (parseInt(gameNo) + 1) + " darf nicht bearbeitet werden.</p>"
                document.getElementById("msgEdit").innerHTML = p;

                document.getElementById("scoreP1").style.backgroundColor = "#ccc";
                document.getElementById("scoreP2").style.backgroundColor = "#ccc";
            } else { //Spiel darf bearbeitet werden
                if (app.clientTournament.gameList[gameNo].scorePlayer1 == -1 && app.clientTournament.gameList[gameNo].scorePlayer2 == -1) {
                    var erg1 = 0;
                    var erg2 = 0;
                    parseInt(erg1);
                    parseInt(erg2);
                    $("#scoreP1").val(erg1);
                    $("#scoreP2").val(erg2);
                    var p = "<p class='help-block'>Trage das Ergebnis vom Spiel " + (parseInt(gameNo) + 1) + " ein.</p>"
                    document.getElementById("msgEdit").innerHTML = p;
                } else { //Spiel wurde schon bearbeitet.
                    var erg1 = app.clientTournament.gameList[gameNo].scorePlayer1;
                    var erg2 = app.clientTournament.gameList[gameNo].scorePlayer2;
                    parseInt(erg1);
                    parseInt(erg2);
                    $("#scoreP1").val(erg1);
                    $("#scoreP2").val(erg2);
                    var p = "<p class='help-block'>Achtung: Das Spiel " + (parseInt(gameNo) + 1) + " wurde bereits bearbeitet.</p>"
                    document.getElementById("msgEdit").innerHTML = p;
                }
            }
        }
    },

    refreshGameTableGame: function (gameNo) {

        var htmlgame = $(".GameTable tr[number=" + gameNo + "]");
        if (app.clientTournament.gameList[gameNo].scorePlayer1 == "-1" && app.clientTournament.gameList[gameNo].scorePlayer2 == "-1") {
            htmlgame.find('td:eq(2)').html("N/A");
            htmlgame.find('td:eq(3)').html("N/A");
        } else {
            htmlgame.find('td:eq(2)').html(app.clientTournament.gameList[gameNo].scorePlayer1);
            htmlgame.find('td:eq(3)').html(app.clientTournament.gameList[gameNo].scorePlayer2);
        }
        var players = this.getPlayer(gameNo);
        var tmpPlayer1 = players[0].textContent;
        var tmpPlayer2 = players[1].textContent;
        htmlgame.find('td:eq(1)').html(tmpPlayer1);
        htmlgame.find('td:eq(4)').html(tmpPlayer2);
        $("#platzTable tr").slice(1).remove();
        var myTable = document.getElementById("platzTable");
        var myTableChildren = myTable.children;
        var rang = app.clientTournament.fieldSize;
        for (var i = 1; i <= app.clientTournament.losersbracketRoundNumber; i++) {
            var loserRound = app.Client.getLosersbracketRound(i);
            rang = rang - (loserRound.length);
            for (var k = 0; k < loserRound.length; k++) {
                var gameNo = loserRound[k];
                var myRow = document.createElement("tr");
                var myCellPlatz = document.createElement("td");
                var myCellplayer = document.createElement("td");
                myCellPlatz.appendChild(document.createTextNode(rang + 1));
                myRow.appendChild(myCellPlatz);
                if (app.clientTournament.gameList[gameNo].player1 > -1 && app.clientTournament.gameList[gameNo].player1 != null && app.clientTournament.gameList[gameNo].player2 != null && app.clientTournament.gameList[gameNo].player2 > -1) {
                    if (app.clientTournament.gameList[gameNo].scorePlayer1 == -1 && app.clientTournament.gameList[gameNo].scorePlayer2 == -1) { //Spiel noch nicht gespielt
                        loser = document.createTextNode("wird gespielt");
                    }
                    if (app.clientTournament.gameList[gameNo].scorePlayer1 < app.clientTournament.gameList[gameNo].scorePlayer2) {
                        var loser = document.createTextNode(app.clientTournament.competitorsList[app.clientTournament.gameList[gameNo].player1].name);
                    } else if (app.clientTournament.gameList[gameNo].scorePlayer1 > app.clientTournament.gameList[gameNo].scorePlayer2) {
                        var loser = document.createTextNode(app.clientTournament.competitorsList[app.clientTournament.gameList[gameNo].player2].name);
                    }
                    //Tabelle setzen
                    
                    myCellplayer.appendChild(loser);
                    myRow.appendChild(myCellplayer);
                }else{
                    myCellplayer.appendChild(document.createTextNode(""));
                    myRow.appendChild(myCellplayer);
                }

                myTableChildren[1].appendChild(myRow);


                //Sonderfall Finale

                if (loserRound[k] == (app.clientTournament.final - 1)) {
                    var gameNo = app.clientTournament.final;
                    var myRow = document.createElement("tr");
                    var myCellPlatz = document.createElement("td");
                    var myCellplayer = document.createElement("td");
                    var winner = document.createTextNode("");
                    var loser = document.createTextNode("");
                    myCellPlatz.appendChild(document.createTextNode(rang));
                    myRow.appendChild(myCellPlatz);
                    if (app.clientTournament.gameList[gameNo].player1 > -1 && app.clientTournament.gameList[gameNo].player1 != null && app.clientTournament.gameList[gameNo].player2 != null && app.clientTournament.gameList[gameNo].player2 > -1) {

                        loser = document.createTextNode("wird gespielt");
                        winner = document.createTextNode("wird gespielt");

                        if (app.clientTournament.gameList[gameNo].scorePlayer1 < app.clientTournament.gameList[gameNo].scorePlayer2) {
                            loser = document.createTextNode(app.clientTournament.competitorsList[app.clientTournament.gameList[gameNo].player1].name);
                            winner = document.createTextNode(app.clientTournament.competitorsList[app.clientTournament.gameList[gameNo].player2].name);
                        } else if (app.clientTournament.gameList[gameNo].scorePlayer1 > app.clientTournament.gameList[gameNo].scorePlayer2) {
                            loser = document.createTextNode(app.clientTournament.competitorsList[app.clientTournament.gameList[gameNo].player2].name);
                            winner = document.createTextNode(app.clientTournament.competitorsList[app.clientTournament.gameList[gameNo].player1].name);
                        }
                    }
                    var myRowLoser = document.createElement("tr");
                    var myRowWinner = document.createElement("tr");
                    var myCellPlatzL = document.createElement("td");
                    var myCellplayerL = document.createElement("td");
                    var myCellPlatzW = document.createElement("td");
                    var myCellplayerW = document.createElement("td");

                    myCellPlatzL.appendChild(document.createTextNode(rang));
                    myCellplayerL.appendChild(loser);
                    myRowLoser.appendChild(myCellPlatzL);
                    myRowLoser.appendChild(myCellplayerL);
                    myTableChildren[1].appendChild(myRowLoser);

                    myCellPlatzW.appendChild(document.createTextNode(rang - 1));
                    myCellplayerW.appendChild(winner);
                    myRowWinner.appendChild(myCellPlatzW);
                    myRowWinner.appendChild(myCellplayerW);
                    myTableChildren[1].appendChild(myRowWinner);
                }
            }
        }
    },

    createGameTable: function (round, firstGame, countGame) {

        function setGameTable(myTable, myRoundFirstGame, countGame) {

            var myTableChildren = myTable.children;

            for (var i = 0; i < countGame; i++) {

                var gameNo = myRoundFirstGame + i;
                var tmpscore1 = document.createTextNode(app.clientTournament.gameList[gameNo].scorePlayer1);
                var tmpscore2 = document.createTextNode(app.clientTournament.gameList[gameNo].scorePlayer2);

                if (tmpscore1.textContent == "-1" && tmpscore2.textContent == "-1") {
                    tmpscore1 = document.createTextNode("N/A");
                    tmpscore2 = document.createTextNode("N/A");
                }
                var tmpPlayer = app.TournamentViews.getPlayer(gameNo);

                var myRow = document.createElement("tr");
                myRow.setAttribute("number", gameNo);
                var myCellSpielnr = document.createElement("td");
                var myCellplayer1 = document.createElement("td");
                var myCellscore1 = document.createElement("td");
                var myCellscore2 = document.createElement("td");
                var myCellplayer2 = document.createElement("td");
                myCellSpielnr.appendChild(document.createTextNode(gameNo + 1));
                myCellplayer1.appendChild(tmpPlayer[0]);
                myCellscore1.appendChild(tmpscore1);
                myCellscore2.appendChild(tmpscore2);
                myCellplayer2.appendChild(tmpPlayer[1]);
                myRow.appendChild(myCellSpielnr);
                myRow.appendChild(myCellplayer1)
                myRow.appendChild(myCellscore1);
                myRow.appendChild(myCellscore2);
                myRow.appendChild(myCellplayer2);
                myTableChildren[1].appendChild(myRow);

                document.getElementById("umgebung_tabelle").appendChild(myTable);
            }
        }

        var myTable = document.createElement("table");
        myTable.setAttribute("class", "table GameTable");
        myTable.setAttribute("id", round);

        var myCaption = document.createElement("caption");
        myCaption.appendChild(document.createTextNode(round));
        myTable.appendChild(myCaption);
        myTable.appendChild(document.createElement("tbody"));

        setGameTable(myTable, firstGame, countGame);
    },

    getPlayer: function (gameNo) {
        var tmpPlayer = [];
        tmpPlayer[0] = app.clientTournament.gameList[gameNo].player1;
        if (tmpPlayer[0] == null) {
            var source = app.clientTournament.gameList[gameNo].source1;
            if (source.asWinner) {
                tmpPlayer[0] = document.createTextNode("Gewinner aus Spiel: " + (source.game + 1))
            } else {
                tmpPlayer[0] = document.createTextNode("Verlierer aus Spiel: " + (source.game + 1))
            }
        } else if (tmpPlayer[0] == -1) {
            tmpPlayer[0] = document.createTextNode("Freilos");
        } else {
            tmpPlayer[0] = document.createTextNode(app.clientTournament.competitorsList[tmpPlayer[0]].name);
        }
        tmpPlayer[1] = app.clientTournament.gameList[gameNo].player2;
        if (tmpPlayer[1] == null) {
            var source = app.clientTournament.gameList[gameNo].source2;
            if (source.asWinner) {
                tmpPlayer[1] = document.createTextNode("Gewinner aus Spiel: " + (source.game + 1))
            } else {
                tmpPlayer[1] = document.createTextNode("Verlierer aus Spiel: " + (source.game + 1))
            }
        } else if (tmpPlayer[1] == -1) {
            tmpPlayer[1] = document.createTextNode("Freilos");
        } else {
            tmpPlayer[1] = document.createTextNode(app.clientTournament.competitorsList[tmpPlayer[1]].name);
        }
        return tmpPlayer;
    }
};

app.CreateView = {

    initCreateForm: function () {
        $('#form_createTournament').submit(this.submit_createTournament);
        this.initInputfields(8);
        if (app.isTest) {
            $('#form_createTournament input[name="tf_tournamentName"]').val("Test-Turnier");
            $('#form_createTournament input[name="tf_password"]').val("pwd");
        }
        $('#competitorsList div.form-group:last-child input').keydown(this.inputKeyEvent);
        $('#btn_addCompetitor').click(app.CreateView.addCompetitorInput);
    },

    initInputfields: function (number) {
        if (number == undefined) {
            number = 8;
        }
        var inputVal = "";
        $('#competitorsList').empty();
        for (var i = 1; i <= number; i++) {
            if (app.isTest) {
                inputVal = "Spieler " + i;
            }
            $('#competitorsList').append($('<div></div>')
                .addClass('form-group')
                .append($('<label></label>')
                    .addClass('col-sm-2 control-label')
                    .html('Teilnehmer ' + i + ':')
                )
                .append($('<div></div>')
                    .addClass('col-sm-10')
                    .append($('<input/>')
                        .attr('type', 'text')
                        .addClass('form-control')
                        .attr('placeholder', 'Name')
                        .val(inputVal)
                    )
                )
            );
        }

    },

    inputKeyEvent: function (e) {
        if (e.keyCode == 9) {
            return app.CreateView.addCompetitorInput();
        } else if (e.keyCode == 8) {
            return app.CreateView.removeCompetitorInput();
        }
    },

    addCompetitorInput: function () {
        $('#competitorsList div.form-group:last-child input').unbind('keydown');
        var index = $('#competitorsList div.form-group').length + 1;
        $('#competitorsList').append($('<div></div>')
            .addClass('form-group')
            .append($('<label></label>')
                .addClass('col-sm-2 control-label')
                .html('Teilnehmer ' + index + ':')
            )
            .append($('<div></div>')
                .addClass('col-sm-10')
                .append($('<input/>')
                    .attr('type', 'text')
                    .attr('placeholder', 'Name')
                    .addClass('form-control')
                    .keydown(app.CreateView.inputKeyEvent)
                )
            )

        );
    },

    removeCompetitorInput: function () {
        console.log("Remove");
        if ($('#competitorsList div.form-group').length > 1 &&
            $('#competitorsList div.form-group:last-child input').val() == "") {
            $('#competitorsList div.form-group:last-child').remove();
            $('#competitorsList div.form-group:last-child input').keydown(this.inputKeyEvent)
                .focus();
            return false;
        }
    },

    submit_createTournament: function () {
        var competitorArray = [],
            inputPwd = $('#form_createTournament input[name="tf_password"]').val(),
            inputName = $('#form_createTournament input[name="tf_tournamentName"]').val();
        $('#competitorsList div.form-group input').each(function () {
            if ($(this).val() != "") {
                competitorArray.push($(this).val());
                $(this).val('');
            }
        });
        $('#form_createTournament input[name="tf_password"]').val('');
        $('#form_createTournament input[name="tf_tournamentName"]').val('');
        app.Client.adminNewTournament(inputName, inputPwd, competitorArray);
        return false;
    },

};